"""add_snapshot_table_for_drc_sync_protocol

Revision ID: 4217e3f408a8
Revises: 4fb45aa624d9
Create Date: 2025-08-20 01:05:59.787168

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "4217e3f408a8"
down_revision: Union[str, None] = "4fb45aa624d9"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "snapshot",
        sa.Column("id", sa.String(length=50), nullable=False),
        sa.Column("types", sa.String(length=255), nullable=False),
        sa.Column("seq", sa.BigInteger(), nullable=False),
        sa.Column("chunk_total", sa.Integer(), nullable=False),
        sa.Column("object_count", sa.Integer(), nullable=False),
        sa.Column("from_seq", sa.BigInteger(), nullable=True),
        sa.Column("is_deleted", sa.Boolean(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("last_access_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("expire_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.Index("ix_snapshot_id", "id"),
        sa.Index("ix_snapshot_types", "types"),
        sa.Index("ix_snapshot_seq", "seq"),
        sa.Index("ix_snapshot_is_deleted", "is_deleted"),
    )

    # Re-create change_log table if it was dropped in previous migration
    op.create_table(
        "change_log",
        sa.Column(
            "seq",
            sa.BigInteger(),
            server_default=sa.text("nextval('global_seq')"),
            nullable=False,
        ),
        sa.Column("ts", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("type", sa.String(length=50), nullable=False),
        sa.Column("id", sa.String(length=255), nullable=False),
        sa.Column("version", sa.Integer(), nullable=False),
        sa.Column("payload", sa.Text(), nullable=True),
        sa.PrimaryKeyConstraint("seq"),
        sa.Index("ix_change_log_seq", "seq"),
        sa.Index("ix_change_log_type", "type"),
        sa.Index("ix_change_log_id", "id"),
        sa.Index("ix_change_log_version", "version"),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("snapshot")
    op.drop_table("change_log")
    # ### end Alembic commands ###
